var http = require('http');
var util = require("util"),
     url = require('url'),
      qs = require('querystring');

var object = require('./Transport.js');
var TransportObj = new object.Transport();
        /*
        function postRequest(request, response, callback) {
        var queryData = "";
        if(typeof callback !== 'function') return null;

        if(request.method === 'POST') {
            request.on('data', function(data)  {
                queryData += data;
                if(queryData.length > 1e6) {
                    queryData = "";
                    response.writeHead(413, {'Content-Type': 'text/plain'});
                    request.connection.destroy();
                }
            });

            request.on('end', function() {
                response.post = querystring.parse(queryData);
                callback();
            });

        } else {
            response.writeHead(405, {'Content-Type': 'text/plain'});
            response.end();
        }
    };
        */

http.createServer(function (req, res) {
     res.writeHead(200, { 'Content-Type': 'text/xml', 'Access-Control-Allow-Origin': '*' });
    if (req.method === "POST") {
        var data = "";
        
        req.on("data", function (chunk) {           
            data += chunk;
        });

        req.on("end", function () {
            console.log("raw: " + data);

            var json = qs.parse(data);
            res.end(TransportObj.processRequest(json));
            console.log("json: " + json);
        });
    }

   
    // res.end('<root>asd</root>');
}).listen(8080);
